package com.woniu.finance.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.enums.ExceptionCode;
import com.ruoyi.common.exception.CustomException;
import com.woniu.book.domain.Booking;
import com.woniu.finance.feign.ApartmentFeign;
import com.woniu.finance.feign.BookFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.woniu.finance.mapper.ApartFeeMapper;
import com.woniu.finance.domain.ApartFee;
import com.woniu.finance.service.IApartFeeService;

/**
 * 公寓缴费记录Service业务层处理
 *
 * @author yy
 * @date 2023-04-04
 */
@Service
public class ApartFeeServiceImpl implements IApartFeeService {
    @Autowired
    private ApartFeeMapper apartFeeMapper;

    @Autowired
    private BookFeign bookFeign;

    @Autowired
    private ApartmentFeign apartmentFeign;

    /**
     * 查询公寓缴费记录
     *
     * @param id 公寓缴费记录主键
     * @return 公寓缴费记录
     */
    @Override
    public ApartFee selectApartFeeById(Long id) {
        return apartFeeMapper.selectApartFeeById(id);
    }

    /**
     * 查询公寓缴费记录列表
     *
     * @param apartFee 公寓缴费记录
     * @return 公寓缴费记录
     */
    @Override
    public List<ApartFee> selectApartFeeList(ApartFee apartFee) {
        return apartFeeMapper.selectApartFeeList(apartFee);
    }

    /**
     * 新增公寓缴费记录
     *
     * @param apartFee 公寓缴费记录
     * @return 结果
     */
    @Override
    public int insertApartFee(ApartFee apartFee) {
        return apartFeeMapper.insertApartFee(apartFee);
    }

    /**
     * 修改公寓缴费记录
     *
     * @param apartFee 公寓缴费记录
     * @return 结果
     */
    @Override
    public int updateApartFee(ApartFee apartFee) {
        return apartFeeMapper.updateApartFee(apartFee);
    }

    /**
     * 批量删除公寓缴费记录
     *
     * @param ids 需要删除的公寓缴费记录主键
     * @return 结果
     */
    @Override
    public int deleteApartFeeByIds(Long[] ids) {
        return apartFeeMapper.deleteApartFeeByIds(ids);
    }

    /**
     * 删除公寓缴费记录信息
     *
     * @param id 公寓缴费记录主键
     * @return 结果
     */
    @Override
    public int deleteApartFeeById(Long id) {
        return apartFeeMapper.deleteApartFeeById(id);
    }

    @Override
    public Integer batchAdd(List<ApartFee> apartFeeList) {
        return apartFeeMapper.batchInsert(apartFeeList);
    }

    @Override
    public void modifyPid(Long bookId, Integer payType, Integer bedId,BigDecimal realPay) {
        // 根据订单号查询订单微服务,校验应该付款的金额
        Booking bookById = bookFeign.getBookById(bookId);
        Integer bookStatus = bookById.getBookStatus();
        BigDecimal realPayFromBooking = bookById.getRealPay();
        if(bookStatus != 1){
            throw new CustomException(ExceptionCode.INVALID_PAY);
        }

        if(!(realPayFromBooking.compareTo(realPay) == 0)){
            throw new CustomException(ExceptionCode.ORDER_PRICE_EXCEPTION);
        }

        if (payType == 0) {
            apartFeeMapper.modifyPid0(bookId);
        } else if (payType == 1) {
            apartFeeMapper.modifyPid1(bookId);
        }

        //TODO mq 把收款确认的消息推送给订单微服务

        //远程调用修改订单的状态为2已付款
        bookFeign.editBookingStatus(bookId);
        //远程调用修改床位状态为2已出租
        apartmentFeign.feignEdit(bedId);
    }


}
